#include "studentsql.h"
#include <QMessageBox>
#include <QCoreApplication>
#include <QSqlQuery>
#include <QtDebug>
#include <QSqlError>
StudentSql * StudentSql::ptrstudSql = nullptr;
StudentSql::StudentSql(QObject *parent)
    : QObject{parent}
{
    init();
}

void StudentSql::init()
{
    if (QSqlDatabase::drivers().isEmpty())
        qDebug()<<"No database drivers found";

    m_db = QSqlDatabase::addDatabase("QSQLITE");
#if 0
    auto str = QCoreApplication::applicationDirPath()+"/data.db";
    qDebug()<<str;
#endif
    auto str = "F:\\data.db";
    m_db.setDatabaseName(str);//设置数据库路径
    if(!m_db.open())
        qDebug()<<"db not open";
}

QList<StudentInfo> StudentSql::getResult(QString Sql)
{
    QList<StudentInfo> l;
    QSqlQuery sql(m_db);
    sql.exec(Sql);
    StudentInfo info;
    while(sql.next())
    {
        info.s_id=sql.value(0).toString();
        info.s_name=sql.value(1).toString();
        //qDebug()<<info.s_name;
        info.s_age=sql.value(2).toUInt();
        info.s_sex=sql.value(3).toString();
        info.s_class=sql.value(4).toString();
        info.s_dept=sql.value(5).toString();
        info.s_addr=sql.value(6).toString();
        l.push_back(info);
    }
    return l;
}

